<?php
namespace Wap\Model;
use Think\Model;

class PraiseItemModel extends Model
{

    protected $tableName = "praise_item";
    protected $patchValidate = true;
    protected $_validate = array(
        array("uid", "require", "用户不存在", 1, '', 1),//上传者ID;在新增字段的时候进行验证
        array("teacher_telephone", "/(^(13|14|15|18|17)\d{9}$)/", "手机号码格式不正确", self::VALUE_VALIDATE, "regex", self::MODEL_BOTH),//指导教师的电话号码，如果填写了就进行验证
        //array( "card", "", "身份证号不唯一", 1, 'unique', 1 ),
        array("pic", "/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(.jpg|.JPG)$/", "没有作品", self::MUST_VALIDATE, "regex", self::MODEL_BOTH),//微信环境内只能上传jpg
        //array( "contract", "require", "没有作品简介 ", 1, "" ),
        array("show_name", "require", "没有作品名称", 1, ""),
        array("author_name", "require", "作者名字不能为空", 1, ""),
        //array( "grade", array( 4, 5, 6, 7, 8 ), "年级非法", 1, "in" ),
        //array( "birth_time", "require", "出生日期不能为空", 1, "" ),
        array("birth_file", "/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(.jpg|.JPG)$/", "出生证明不能为空", self::MUST_VALIDATE, "regex", self::MODEL_BOTH),//jpg
        array("author_pic", "/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(.jpg|.JPG)$/", "提供的作者图片正确", self::VALUE_VALIDATE, "regex", self::MODEL_BOTH),//jpg
        array("teacher_pic", "/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(.jpg|.JPG)$/", "提供的教师图片不正确", self::VALUE_VALIDATE, "regex", self::MODEL_BOTH),//jpg
        array("school_pic", "/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(.jpg|.JPG)$/", "提供的学校图片不正确", self::VALUE_VALIDATE, "regex", self::MODEL_BOTH),//jpg
        array("school_name", "require", "学校名字不能空", 1, ""),
        array('provience', 'require', '省不能为空', 1),
        array('city', 'require', '市不能为空', 1),
        array('region', 'require', '区不能为空', 1),
        //array( "address", "require", "地址不能空", 1 ),
    );

    public function insert($datas, $condition)
    {
        $result = array("success" => false, "data" => array(), "validate" => array());

        $id = 0;
        $validated = $this->create($datas);
        if (!$validated) {
            $result['validate'] = $this->getError();
            return $result;
        } else if (isset($datas['id'])) {
            $condition['id'] = $datas['id'];
            $id = $this->where($condition)->save($datas);
        } else {
            $id = $this->add($datas);
        }

        if ($id > 0 && !isset($datas['id'])) {
            $result['success'] = true;
            $result['data'] = $this->where("id=%d", array($id))->select();
        } else if ($id > 0 && isset($datas['id'])) {
            $result['success'] = true;
            $result['data'] = $this->where('id=%d', array($datas['id']))->select();
        } else if ($id === 0) {
            $result['success'] = true;//TODO: 是否会导致系统错误?
        }
        return $result;
    }

    public function fetch($conditions, $page = 1, $limit = 6)
    {
        $select = array('esm_praise_item.id as id, esm_praise_item.grade as grade, esm_praise_item.pic as pic, esm_praise_item.contract, esm_praise_item.show_name as show_name, esm_praise_item.votes as votes, esm_praise_item.share as share');
        $count = ceil($this->where($conditions)->count() / $limit);
        $datas = $this->field($select)
            ->where($conditions)
            ->order("votes desc,grade asc")
            ->page($page, $limit)
            ->select();

        $result = array(
            "count" => $count,//总共的页数
            "curren" => $page,//当前页
            "next" => $page + 1,//
            "pre" => $page - 1,//
            "size" => count($datas), //当前页数据个数
            "datas" => $datas//查询到的数据
        );
        return $result;
    }

    public function fetcheat($conditions, $page = 1, $limit = 6)
    {
        $select = array('esm_praise_item.id as id, esm_praise_item.grade as grade, esm_praise_item.pic as pic, esm_praise_item.contract, esm_praise_item.show_name as show_name, esm_praise_item.votes as votes, esm_praise_item.share as share');
        $count = ceil($this->where($conditions)->count() / $limit);
        $datas = $this->field($select)
            ->where($conditions)
            ->order("share desc,grade asc")
            ->page($page, $limit)
            ->select();

        $result = array(
            "count" => $count,//总共的页数
            "curren" => $page,//当前页
            "next" => $page + 1,//
            "pre" => $page - 1,//
            "size" => count($datas), //当前页数据个数
            "datas" => $datas//查询到的数据
        );
        return $result;
    }

    public function fetch_by_id($id = -1)
    {
        $select = array('esm_praise_item.id as id, esm_praise_item.grade as grade, esm_praise_item.praise_id as praise_id, 
        esm_praise_item.pic as pic, esm_praise_item.contract, esm_praise_item.show_name as show_name, esm_praise_item.votes as votes,
         esm_praise_item.author_name as author_name, esm_praise_item.teacher_name as teacher_name, esm_praise_item.author_contract,
          esm_praise_item.school_name, esm_praise_item.school_contract, esm_praise_item.teacher_contract, esm_praise_item.author_pic,
           esm_praise_item.teacher_pic, esm_praise_item.school_pic, esm_support_team.name as support_team,
            esm_praise_item.external_link,esm_praise_item.click,esm_praise_item.yuliu');

        $conditions['esm_praise_item.id'] = $id;
        $datas = $this->field($select)
            ->join('LEFT JOIN __SUPPORT_TEAM__ ON __SUPPORT_TEAM__.id = __PRAISE_ITEM__.support_team')
            ->where($conditions)
            ->find();

        return $datas;
    }

    public function fetch_rank_by_id($id)
    {
        $rows = $this->where("id = %d", array($id))->find();
        $votes = $rows['votes'];
        $praise = $rows['praise_id'];
        $result = $this->fetch_rank_by_votes($votes, $praise);
        $result['votes'] = $votes;

        return $result;
    }

    public function fetch_rank_by_votes($votes, $praise)
    {
        $rank = $this->distinct(true)->field("votes")->where("votes > %d AND praise_id = %d", array($votes, $praise))->count();
        $last = $this->distinct(true)->field("votes")->where("votes > %d AND praise_id = %d", array($votes, $praise))->order("votes asc")->limit(1)->find();

        $result = array("index" => $rank + 1, "gap" => 0, "percent" => '100%');
        if (count($last) > 0) {
            $result['gap'] = $last['votes'] - $votes;
            $result['percent'] = ($votes / $last['votes'] * 100) . "%";
        }
        return $result;
    }

    public function update_votes($id)
    {
        //$this->startTrans();
        $updated_a = $this->where("id = %d", array($id))->setInc("votes", 1);//本作品
//        $this->where(['id' => 13142])->setInc("votes", 1);
        //sleep( 1 );
        if (!$updated_a) {
            //$this->rollback();
            return false;
        } else {
            //$this->commit();
            return true;
        }
    }

    public function eat_update_votes($id)
    {
        //$this->startTrans();
        $updated_a = $this->where("id = %d", array($id))->setInc("votes", 1);//本作品
        $updated_b = $this->where("id = %d", array($id))->setInc("share", 1);//本作品
        //sleep( 1 );
        if (!$updated_a || !$updated_b) {
            //$this->rollback();
            return false;
        } else {
            //$this->commit();
            return true;
        }
    }

    public function update_share_by_comment($id)
    {
        $updated_b = $this->where("id = %d", array($id))->setInc("share", 5);//本作品
        if (!$updated_b) {
            //$this->rollback();
            return false;
        } else {
            //$this->commit();
            return true;
        }
    }
}

?>
